package com.maoslv.security;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.Assert;

import java.util.Collection;

public class CustomUser implements UserDetails {

    private Integer id;

    private String password;
    private final String username;
    //权限
    private final Collection<? extends GrantedAuthority> authorities;
    private final boolean accoutNonExpired;
    private final boolean accountNoLocked;
    private final boolean credentialsNonExpired;
    private final boolean enabled;

    public CustomUser(Integer id,String username, String password, Collection<? extends GrantedAuthority> authorities) {
        this(id,username,password,true,true,true,true, authorities);
    }

    public CustomUser(Integer id, String username, String password, boolean enabled, boolean accoutNonExpired,
                      boolean credentialsNonExpired, boolean accountNoLocked,
                      Collection<? extends GrantedAuthority> authorities) {
        Assert.isTrue(username != null && !"".equals(username) && password != null,
                "Cannot pass null or empty values to constructor");
        this.id = id;
        this.username = username;
        this.password = password;
        this.enabled = enabled;
        this.accountNoLocked= accountNoLocked;
        this.credentialsNonExpired = credentialsNonExpired;
        this.accoutNonExpired = accoutNonExpired;
        this.authorities = authorities;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.accoutNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.accountNoLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.credentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
}
